<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <h1>
      Promise.all() :并发处理多个异步请求，返回每个异步请求的结果
      <br />
      Promise.race()：并发处理异步请求，返回最快的完成的结果，只要有一个完成就返回结果
      Promise.resolve,
      <br />
      Promise.reject
    </h1>
    <pre>
        then ,finally, catch
    </pre>
    <script>
      get = (url) => {
        let ajax = new Promise((resolve, reject) => {
          let xhr = new XMLHttpRequest()
          xhr.open('get', url)
          xhr.send()
          xhr.onreadystatechange = function () {
            if (xhr.readyState != 4) return
            if (xhr.readyState === 4 && xhr.status == 200) {
              resolve(xhr.responseText)
            } else {
              reject('error')
            }
          }
        })
        return ajax
      }
      get('http://www.liulongbin.top:3006/api/getbooks')
        .then((result) => {
          var arr = JSON.parse(result)
          console.log(arr)

          console.log(JSON.parse(result))

          console.log(JSON.parse(result).data[0].id)
        })
        .catch((err) => {
          console.log(err)
        })
    </script>
  </body>
</html>
